package com.googlecode.connectlet.crypto.cipher.whitening;

import com.googlecode.connectlet.crypto.cipher.CipherFactory;
import com.googlecode.connectlet.crypto.cipher.Decipheror;
import com.googlecode.connectlet.crypto.cipher.Encipheror;
import com.googlecode.connectlet.util.Bytes;

public class WhiteningFactory implements CipherFactory {
	private CipherFactory cipherFactory;
	private int blockSize;

	public WhiteningFactory(CipherFactory cipherFactory) {
		this.cipherFactory = cipherFactory;
		blockSize = cipherFactory.getBlockSize();
	}

	@Override
	public int getBlockSize() {
		return blockSize;
	}

	@Override
	public Encipheror getEncipheror(byte[] key) {
		byte[] key1 = Bytes.left(key, key.length - blockSize * 2);
		byte[] key2 = Bytes.right(key, blockSize * 2);
		return new WhiteningEnc(cipherFactory.getEncipheror(key1), key2);
	}

	@Override
	public Decipheror getDecipheror(byte[] key) {
		byte[] key1 = Bytes.left(key, key.length - blockSize * 2);
		byte[] key2 = Bytes.right(key, blockSize * 2);
		return new WhiteningDec(cipherFactory.getDecipheror(key1), key2);
	}
}